package com.example.day3;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.CountDownTimer;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    int progress = 20;
    TextView text ;
    String id = "my_channel_01";
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    text = findViewById(R.id.text10);
    text.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,"发送通知",Toast.LENGTH_LONG).show();
//            notification10();
            notification1();
        }
    });
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelName = "channel_name";
            NotificationChannel channel = new NotificationChannel(id, channelName, NotificationManager.IMPORTANCE_HIGH);
            NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
            manager.createNotificationChannel(channel);
        }


//        if (!NotificationsUtils.isNotificationEnabled(this)) {
//            final AlertDialog dialog = new AlertDialog.Builder(this).create();
//            dialog.show();
//
//            View view = View.inflate(this, R.layout.dialog, null);
//            dialog.setContentView(view);
//
//            TextView context = (TextView) view.findViewById(R.id.tv_dialog_context);
//            context.setText("检测到您没有打开通知权限，是否去打开");
//
//            TextView confirm = (TextView) view.findViewById(R.id.btn_confirm);
//            confirm.setText("确定");
//            confirm.setOnClickListener(new View.OnClickListener() {
//                public void onClick(View v) {
//                    dialog.cancel();
//                    Intent localIntent = new Intent();
//                    localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    if (Build.VERSION.SDK_INT >= 9) {
//                        localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
//                        localIntent.setData(Uri.fromParts("package", MainActivity.this.getPackageName(), null));
//                    } else if (Build.VERSION.SDK_INT <= 8) {
//                        localIntent.setAction(Intent.ACTION_VIEW);
//
//                        localIntent.setClassName("com.android.settings",
//                                "com.android.settings.InstalledAppDetails");
//
//                        localIntent.putExtra("com.android.settings.ApplicationPkgName",
//                                MainActivity.this.getPackageName());
//                    }
//                    startActivity(localIntent);
//                }
//            });
//
//            TextView cancel = (TextView) view.findViewById(R.id.btn_off);
//            cancel.setOnClickListener(new View.OnClickListener() {
//                public void onClick(View v) {
//                    dialog.cancel();
//                }
//            });
//        }else{
//            Toast.makeText(MainActivity.this,"已打开通知权限",Toast.LENGTH_LONG).show();
//        }


    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)

    //直接回复通知
    public void notification9( ) {
        // 初始化NotificationManager
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // 创建通知

        NotificationCompat.Builder builder =  new NotificationCompat.Builder(this);
        builder.setTicker("aaaaaaaaa");
        builder.setContentInfo("附加消息");
        builder.setContentText("bbbbbbbb");
        builder.setContentTitle("cccccccc");
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setAutoCancel(true);
        // 设置通知的优先级(悬浮通知)
        builder.setPriority(NotificationCompat.PRIORITY_MAX);


        builder.setWhen(System.currentTimeMillis());
        PendingIntent pendIntent = PendingIntent.getActivity(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendIntent);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//            adaptAndroidN(context, builder, user);
//        }
//        messageNotification = builder.build();
//        messageNotification.flags = Notification.FLAG_AUTO_CANCEL;
//        messageNotificatioManager.notify(notifyId, messageNotification);
    }

    // TODO: 2019/7/8 分组通知
    //分组通知
    public void notification10(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelID = "1";
            String channelName = "channel_name";

            String id = "my_channel_01";
            String name="我是渠道名字";
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            Notification notification = null;


                NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
                notificationManager.createNotificationChannel(mChannel);


            Notification.Builder mBuilder0 = new Notification.Builder(this);
            NotificationCompat.Builder mBuilder1 = new NotificationCompat.Builder(this);
            NotificationCompat.Builder mBuilder2 = new NotificationCompat.Builder(this);
            NotificationCompat.Builder mBuilder3 = new NotificationCompat.Builder(this);
            mBuilder0.setSmallIcon(R.mipmap.ic_launcher);
            mBuilder1.setSmallIcon(R.mipmap.ic_launcher);
            mBuilder2.setSmallIcon(R.mipmap.ic_launcher);
            mBuilder3.setSmallIcon(R.mipmap.ic_launcher);
            mBuilder0.setGroup("2");
            mBuilder0.setGroupSummary(true);
            mBuilder1.setGroup("2");
//        mBuilder1.setGroupSummary(true);
            mBuilder2.setGroup("2");
//        mBuilder2.setGroupSummary(true);
            mBuilder3.setGroup("2");
//        mBuilder3.setGroupSummary(true);

            mBuilder0.setTicker("自己的APP发送的消息");
            mBuilder0.setContentText("1111");
            mBuilder0.setContentTitle("fuweeri");
            mBuilder1.setTicker("提示收到了一条消息");
            mBuilder1.setContentText("1111bbbb");
            mBuilder1.setContentTitle("天气预报1111");
            mBuilder2.setTicker("提示收到了二条消息");
            mBuilder2.setContentText("1111cccc");
            mBuilder2.setContentTitle("天气预报222");
            mBuilder3.setTicker("提示收到了三条消息");
            mBuilder3.setContentText("1111dddd");
            mBuilder3.setContentTitle("天气预报333333");
            notificationManager.notify(0, mBuilder0.build());
            notificationManager.notify(1, mBuilder1.build());
            notificationManager.notify(2, mBuilder2.build());
            notificationManager.notify(3, mBuilder3.build());
        }
    }

    // TODO: 2019/7/8 普通通知
    public void notification1() {
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //通知的构建对象
        NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
//        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.bq1);
        //int -Bitmap
//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bq2);
//        builder.setLargeIcon(bitmap);
        builder.setChannelId(id);
        builder.setTicker("提示收到了一条消息");
        builder.setContentTitle("天气预报");
        builder.setContentText("正文，今天天气晴，东南风转西北风5678级");
        builder.setContentInfo("附加消息");
        builder.setDefaults(Notification.DEFAULT_ALL);//震动、呼吸灯闪、声音
//        builder.setAutoCancel(true);//点一下通知关闭

        //设置通知栏点击意图

//      intent - 点击跳转
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent intent1 = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_ONE_SHOT);
        builder.setContentIntent(intent1);
        //////////////////////////
        Notification notification = builder.build();
        manager.notify(200, notification); //发送通知

//        Notification.Builder builder2 = new Notification.Builder(this);
//        builder2.setSmallIcon(R.mipmap.bq1);
//        //int -Bitmap
////        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bq2);
////        builder.setLargeIcon(bitmap);
//        builder2.setTicker("提示收到了ba条消息");
//        builder2.setContentTitle("天气预报222");
//        builder2.setContentText("1111正文，今天天气晴，东南风转西北风5678级");
//        builder2.setContentInfo("附加消息");
//        builder2.setDefaults(Notification.DEFAULT_ALL);//震动、呼吸灯闪、声音
//        builder2.setAutoCancel(true);//点一下通知关闭
//        builder2.setGroup("1");
//        Notification notification2 = builder2.build();
//        manager.notify(100, notification2); //发送通知
    }

    // TODO: 2019/7/8 进度条通知
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void notification2() {
        final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        final Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.bq3);
        builder.setContentTitle("进度条通知");
        manager.notify(3, builder.build());


        CountDownTimer countDownTimer = new CountDownTimer(11000, 1000) {
//            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onTick(long millisUntilFinished) {
                progress += 10;
                builder.setProgress(100, progress, true);
                manager.notify(3, builder.build()); //通知id 2
            }

            @Override
            public void onFinish() {
                manager.cancel(3);
            }
        }.start();


    }

    // TODO: 2019/7/8 自定义通知
    @TargetApi(Build.VERSION_CODES.N)
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void notification3() {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.bq5);
        builder.setContentTitle("自定义通知");

        //xxx.layout -
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout);
        remoteViews.setTextViewText(R.id.textNotifi, "代码中赋值");
        remoteViews.setImageViewResource(R.id.imageNotifi, R.mipmap.bq1);
       // builder.setContent(remoteViews);
        builder.setCustomContentView(remoteViews);
        manager.notify(4, builder.build());
    }

    // TODO: 2019/7/8 大图通知
    public void notification4(){
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.bq3);
        builder.setContentTitle("大图通知");
        Notification.BigPictureStyle bitPicStyle = new Notification.BigPictureStyle();
//        NotificationCompat.BigTextStyle  bigTextStyle =new NotificationCompat.BigTextStyle();
        bitPicStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.aaa));
        builder.setStyle(bitPicStyle);
        manager.notify(4, builder.build());
    }

    // TODO: 2019/7/8 列表通知 +悬挂
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void notification5() {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.bq3);
        builder.setContentTitle("列表通知");

//        Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
//        bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.bq2));

        Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
        inboxStyle.addLine("妙蛙种子");
        inboxStyle.addLine("皮卡丘");
        inboxStyle.addLine("王国桦");

        builder.setStyle(inboxStyle);
        //不能跨APP
        Intent intent = new Intent(this, MainActivity.class);
        //intent - PendingIntent
        PendingIntent intent1 = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_ONE_SHOT);
        //悬挂
        builder.setFullScreenIntent(intent1, true);

        builder.setContentIntent(intent1);
        manager.notify(9, builder.build());
    }

    @TargetApi(Build.VERSION_CODES.O)
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void notification6() {

        String id = "test1";
        String name = "name";
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
        mNotificationManager.createNotificationChannel(mChannel);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.bq3);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.bq3));
        builder.setAutoCancel(true);
        builder.setContentTitle("悬挂式通知");
        Intent intent = new Intent(this,Main2Activity.class);
        builder.setChannelId(id);
        mNotificationManager.notify(2, builder.build());



//        Intent intent = new Intent(this,Main2Activity.class);
//        PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
//
//        String id = "channel_001";
//        String name = "name";
//        NotificationManager notificationManager = (NotificationManager)
//                this.getSystemService(NOTIFICATION_SERVICE);
//        Notification notification = null;
////        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//判断API
//            NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
//            notificationManager.createNotificationChannel(mChannel);
//             notification = new Notification.Builder(this)
//                    .setChannelId(id)
//                    .setContentTitle("通知")
//                    .setContentText("你有一个新的通知")
////                    .setContentIntent(pi)
//                    .setLights(Color.GREEN, 1000, 1000)//设置三色灯
//                    .setSmallIcon(R.mipmap.ic_launcher_round).build();
//        }else{
//            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
//                    .setContentTitle("通知")
//                    .setContentText("你有新的通知")
//                    .setSmallIcon(R.mipmap.ic_launcher_round)
//                    .setOngoing(true)
//                    .setContentIntent(pi)
//                    .setLights(Color.GREEN, 1000, 1000)//设置三色灯
//                    .setChannelId(id);//无效
//
//            notification = notificationBuilder.build();
//        }
//        notificationManager.notify(1,notification)
//        Toast toast = new Toast(this);
//        toast.setView(View v);
//        toast.setDuration(Toast.LENGTH_SHORT);
//        toast.show();



    }
}
